<?php
/**
 * Created by PhpStorm.
 * User: fengbob
 * Date: 2018/2/19
 * Time: 10:28
 */

namespace app\models;


use app\librarys\Tools;
use app\models\db\EShop;
use yii\db\Query;

class EShopExt extends EShop
{
    static $mini_program = '1';
    public static function getEshop($page = 0, $pageSize = 20, $name = ''){
        if ($page <= 0) $page = 0;
        $start = $page * $pageSize;

        $query = EShop::find()
            ->where(['is_on'=>1,'mini_programs_id'=>EShopExt::$mini_program])
            ->andFilterWhere(['like', 'name', $name])
            ->orderBy('create_datetime desc')
            ->limit($pageSize)
            ->offset($start)
            ->all();
        return $query;
    }

    /**
     * 数量
     * @param string $name
     * @return int|string
     */
    public static function getEshopCount($name = '')
    {
        return db\EShop::find()
            ->where(['mini_programs_id'=>EShopExt::$mini_program])
            ->andFilterWhere(['like', 'name', $name])
            ->count();
    }


    /**
     * 列表
     * @param int $page
     * @param int $pageSize
     * @param string $name
     * @return mixed
     */
    public static function getEshopList($page = 0, $pageSize = 20, $name = '')
    {
        if ($page <= 0) $page = 0;
        $start = $page * $pageSize;

        $query = EShop::find()
            ->where(['mini_programs_id'=>EShopExt::$mini_program])
            ->andFilterWhere(['like', 'name', $name])
            ->orderBy('create_datetime desc')
            ->limit($pageSize)
            ->offset($start)
            ->all();
        return $query;

    }

    /**
     *  根据id获取行
     * @param $id
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getEshopById($id)
    {
       $query = EShop::findOne($id);
        return $query;
    }


    /**
     * @param $id
     * @param $name
     * @param $logo_id
     * @param $brief_introduction
     * @param $cert_url
     * @param $key_url
     * @param $root_url
     * @param $is_main
     * @param $is_on
     * @return bool
     */
    public static function saveEshop($mini_programs_is,$id, $name,$logo_id,$brief_introduction,
                                     $cert_url,$key_url,$root_url,$is_main,$is_on,$status,$sort,$contract_man,$contact_phone,
                                      $begin_datatime,$end_datetime)
    {
       $item = EShop::findOne($id);
       if(empty($item)) {
           $item = new EShop();
           $item->id = Tools::uuid();
       }
        $item->mini_programs_id = $mini_programs_is;
       $item->name = $name;
       $item->logo_url  =  $logo_id;
        $item->brief_introduction  =  $brief_introduction;
        $item->cert_url  =  $cert_url;
        $item->key_url  =  $key_url;
        $item->root_url  =  $root_url;
        $item->is_main  =  $is_main;
        $item->is_on  =  $is_on;
        $item->sort = $sort;
        $item->status = $status;
        $item->contact_man = $contract_man;
        $item->contact_phone = $contact_phone;
        $item->begin_datetime = $begin_datatime;
        $item->end_datetime = $end_datetime;
        $result = $item->save();

        return $result;
    }



    public static function deleteEshop($id)
    {
        $item = EShop::findOne($id);
        $item->is_on = 0;
        $result = $item->save();
        return $result;
    }

    /**
     * 查找子店铺
     * @param  int $mini_programs_id [description]
     * @param  integer $page [description]
     * @param  integer $pageSize [description]
     * @return array [description]
     * @author milloc
     * @date   2018-04-16
     */
    public function getSubEshopList($mini_programs_id, $page = 0, $page_size = 20)
    {
        $mini_programs_id = empty($mini_programs_id) ? self::$mini_program : $mini_programs_id;
        $page_size = $page_size > 0 ? $page_size : 0;
        $start = $page > 0 ? $page * $page_size : 0;

        $list = (new Query)->from(Eshop::tableName())->select(['id', 'logo_url', 'name'])->where(['mini_programs_id' => $mini_programs_id])->andWhere(['or', ['is_main' => 0], 'isnull(`is_main`)'])->andWhere(['is_on' => 1])->orderBy('create_datetime desc')->limit($page_size)->offset($start)->all();
        // 主要销售产品
        $shop_id = array_column($list, 'id');
        $major_arr = (new Query)->from('{{t_e_shop_major_sales}} m')->select(['m.e_shop_id', 'c.name'])->leftJoin('{{t_goods_category}} c', 'c.id = m.goods_category_id')->where(['m.e_shop_id' => $shop_id])->all();
        
        $res = [];
        foreach ($list as $item) {
            $major = array_filter($major_arr, function ($v, $k) use ($item) {
                if (is_array($v) && $v['e_shop_id'] == $item['id']) {
                    return true;
                }
                return false;
            }, ARRAY_FILTER_USE_BOTH);
            $major_arr = array_diff($major_arr, array_column($major, 'e_shop_id'));

            $item['major_sales'] = implode(',', $major);
            $res[] = $item;
        }

        return $res;
    }
}